package com.hongyi.quartz.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.quartz.domain.SysJobLog;
import com.hongyi.quartz.service.ISysJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 *
 * @author DingHao
 */
@Api(tags = "调度日志控制器")
@ApiSort(value = 18)
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseController {

    @Resource
    ISysJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务调度日志分页数据")
    @ApiImplicitParam(name = "sysJobLog" , value = "日志对象" , dataTypeClass = SysJobLog.class, paramType = "query")
    @SaCheckPermission("monitor:job:list")
    @GetMapping(value = "/list")
    public PageData<SysJobLog> list(SysJobLog sysJobLog) {
        return jobLogService.selectJobLogList(sysJobLog);
    }

    /**
     * 导出定时任务调度日志列表(返回流)
     *
     * @param sysJobLog 日志对象
     * @param response  返回
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "导出定时任务调度日志(返回流)")
    @ApiImplicitParam(name = "sysJobLog" , value = "日志对象" , dataTypeClass = SysJobLog.class, paramType = "query")
    @SaCheckPermission("monitor:job:export")
    @Log(title = "任务调度日志" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/exportByStream" , produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportStream(SysJobLog sysJobLog, HttpServletResponse response) {

    }

    /**
     * 根据调度编号获取详细信息
     *
     * @param id 调度编号
     * @return 结果
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据调度编号获取详细信息")
    @ApiImplicitParam(name = "id" , value = "调度编号" , dataTypeClass = Long.class, paramType = "path")
    @SaCheckPermission("monitor:job:query")
    @GetMapping(value = "/{id}")
    public JsonResult<SysJobLog> getInfo(@PathVariable Long id) {
        return JsonResult.ok(this.jobLogService.selectJobLogById(id));
    }


    /**
     * 删除定时任务调度日志
     *
     * @param ids 调度编号数组
     * @return 结果
     */
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "id" , value = "调度编号" , dataTypeClass = Long.class, allowMultiple = true, paramType = "path")
    @SaCheckPermission("monitor:job:remove")
    @Log(title = "定时任务调度日志" , businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public JsonResult<Void> remove(@PathVariable Long[] ids) {
        return toResult(this.jobLogService.deleteJobLogByIds(ids));
    }

    /**
     * 清空定时任务调度日志
     *
     * @return 结果
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "清空定时任务调度日志")
    @SaCheckPermission("monitor:job:remove")
    @Log(title = "调度日志" , businessType = BusinessType.CLEAN)
    @DeleteMapping(value = "/clean")
    public JsonResult<Void> clean() {
        this.jobLogService.cleanJobLog();
        return JsonResult.ok();
    }

}
